package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.ClientEntity.ClientCourse;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.*;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-21
 */
@Api(description = "课程")
@RestController
@RequestMapping("/eduservice/course")
//原来需要在Controller上面加上@CrossOrigin解决跨域问题，现在gateway已经处理了   /*解决跨域问题*/
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;



    @ApiOperation(value = "条件分页查询课程基本信息")
    @PostMapping("/pageCourse/{current}/{limit}")
    public R pageCourse(@PathVariable(value = "current") long current,
                        @PathVariable(value = "limit") long limit,
                        @RequestBody CourseQuery courseQuery){
        return eduCourseService.pageCourse(current,limit,courseQuery);
    }



    @ApiOperation(value = "点击 保存并下一步 执行的方法")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        return eduCourseService.addCourseInfo(courseInfoVo);
    }



    /*根据课程id查询课程里面的基本信息*/
    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }



    /*修改课程信息*/
    @ApiOperation(value = "修改课程基本信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }



    /*根据课程id查询课程的最终发布信息*/
    @ApiOperation(value = "根据课程id查询课程的最终发布信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("coursePublishVo", publishCourseInfo);
    }



    /*点击发布课程按钮执行的方法*/
    @ApiOperation(value = "点击发布课程按钮执行的方法")
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        boolean flag=eduCourseService.publishCourse(id);
        if(flag==true){
            return R.ok();
        }
        return R.error();
    }


    /*根据课程id删除整个课程
    * 包括删除课程基本信息，删除课程章节，课程小节
    *  * */
    @ApiOperation(value = "根据课程id删除课程信息")
    @PostMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable(value = "courseId") String courseId){
        boolean flag=eduCourseService.deleteCourse(courseId);
        if(flag){
            return R.ok();
        }
        return R.error();
    }

    /*前台：条件分页查询课程信息*/
    @ApiOperation(value = "条件分页查询课程信息")
    @PostMapping("/selectPageCourse/{current}/{limit}")
    public R selectPageCourse(@PathVariable(value = "current") long current,
                              @PathVariable(value = "limit") long limit,
                              @RequestBody CourseQueryVo courseQueryVo){
        return eduCourseService.selectPageCourse(current,limit,courseQueryVo);

    }

    /*前台：获取课程的二级分类*/
    @ApiOperation(value = "获取课程的二级分类")
    @GetMapping("/list2")
    public R list2(){
        return eduCourseService.list2();
    }

    /*前台：根据课程id查询课程的详细信息*/
    @ApiOperation(value = "前台里面根据课程id查询课程的详细信息")
    @GetMapping("/selectCourseVo/{courseId}")
    public R selectCourseVo(@PathVariable(value = "courseId") String courseId, HttpServletRequest request){
        return eduCourseService.selectCourseVo(courseId,request);
    }

    /*根据课程id获取课程信息*/
    @ApiOperation(value = "这里是用于远程调用的")
    @GetMapping("/getCourseInfoClient/{courseId}")
    public ClientCourse getCourseInfoClient(@PathVariable(value = "courseId") String courseId){
        return eduCourseService.getCourseInfoClient(courseId);
    }


}

